#include<d3dx9.h>
#include"Sprite.h"
CSprite::CSprite(bool _isstatic, int _sx,int _sy,int width, int height,int cursprite, int lastsprite, int spriteperrow)
{
	this->Height=height;
	this->sx=_sx;
	this->sy=_sy;
	this->Width=width;
	this->Cursprite=cursprite;
	this->Lastsprite=lastsprite;
	this->Spriteperrow=spriteperrow;
	this->isstatic=_isstatic;
	this->ismario=false;
	isinvisible=false;
//	this->Filename=filename;
}
/*void CSprite::LoadResources(LPDIRECT3DDEVICE9 d3ddv)
{
	D3DXIMAGE_INFO info;
	HRESULT result=D3DXGetImageInfoFromFile(Filename,&info);
	if(result!=D3D_OK)
	{
		return;
	}
	D3DXCreateTextureFromFileEx(d3ddv,Filename,info.Width,info.Height,1,D3DPOOL_DEFAULT,D3DFMT_UNKNOWN,D3DPOOL_DEFAULT,D3DX_DEFAULT,D3DX_DEFAULT,D3DCOLOR_XRGB(0,0,0),&info,NULL,&image);

}*/
void CSprite::Render(LPD3DXSPRITE sprite,LPDIRECT3DTEXTURE9 image,int x, int y)
{
	RECT rect;
	if(isstatic==false)
	{
	rect.left=Cursprite*(Width+1)+sx;
	rect.top=Spriteperrow*(Height+1)+sy;
	rect.right=rect.left+Width;
	rect.bottom=rect.top+Height;
	}
	else
	{
		rect.left=sx;
		rect.top=sy;
		rect.right=rect.left+Width;
		rect.bottom=rect.top+Height;
	}
	D3DXVECTOR3 position((float)x,(float)y,0);
	if(isinvisible==true) sprite->Draw(image,&rect,NULL,&position,D3DCOLOR_XRGB(400,400,400));
	else sprite->Draw(image,&rect,NULL,&position,D3DCOLOR_XRGB(255,255,255));
}
void CSprite::Next()
{
	DWORD now=GetTickCount();
	if(ismario==false)
	{
	if(now-last_time>100)
	{
		Cursprite++;
		if(Cursprite>Lastsprite)  Cursprite=0;
		last_time=now;
	}
	}
	else
	{
		if(now-last_time>mario_frame_rate)
	{
		Cursprite++;
		if(Cursprite>Lastsprite) Cursprite=1;
		last_time=now;
	}
	}
}
RECT CSprite::getRECT(int x, int y)
{
	RECT rect;
	rect.left=x;
	rect.top=y;
	rect.right=rect.left+Width;
	rect.bottom=rect.top+Height;
	return rect;
}
void CSprite::Reset()
{
	Cursprite=0;
}
 CSprite::~CSprite()
{
	//if(image!=NULL) image->Release();
}